#pragma once

#include <atlddx.h>

#include "CURLHelper.h"

#define DOWNPROP_MAX_THREAD_DIGITALS	3
#define DOWNPROP_MAX_THREAD				8

class CDownPropertyPage : public CPropertyPageImpl<CDownPropertyPage>,
						   public CWinDataExchange<CDownPropertyPage>
{
public:
	enum { IDD = IDD_DOWN_PROPERTY };

	BEGIN_DDX_MAP(CDownPropertyPage)

		DDX_INT(IDC_DOWNPROP_THREAD, m_nThread)
		DDX_INT_RANGE(IDC_DOWNPROP_THREAD, m_nThread, 1, DOWNPROP_MAX_THREAD)

		//Though it's a radio box, but we only care about the check state
		DDX_CHECK(IDC_RADIO_KEEP_TASK, m_bIsKeepTask);

    END_DDX_MAP()

	BEGIN_MSG_MAP(CDownPropertyPage)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)

		COMMAND_ID_HANDLER(IDC_BT_EXPORT_LINK, OnJumpToExport)
		COMMAND_ID_HANDLER(IDC_BT_BROWS_FOLDER, OnBrowseFolder)

		NOTIFY_HANDLER(IDC_DOWNPROP_THREAD_PIN, UDN_DELTAPOS, OnSpinClick)

		CHAIN_MSG_MAP(CPropertyPageImpl<CDownPropertyPage>)
	END_MSG_MAP()

public:
	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	LRESULT OnJumpToExport(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnBrowseFolder(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

	LRESULT OnSpinClick(int nCtrlId, LPNMHDR lpnmhdr, BOOL& bHandled);

public:
	CDownPropertyPage(void);
	~CDownPropertyPage(void);
	
	void OnDataExchangeError(UINT nCtrlID, BOOL bSave);
	void OnDataValidateError(UINT nCtrlID, BOOL bSave, _XData& data);

	void RefreshSettings();


public:
	CUpDownCtrl m_spThread;
	CEdit	m_edtThread;
	CEdit	m_edtSavePath;
	CButton m_btnIsStartNow, m_btnIsKeepTask;
	CButton m_chkSaveDefault;

	vector<CCURLHelper::PAGE_LINK_ITEM*>* m_pvecLinksData;

	bool m_bIsKeepTask;
	INT m_nThread;
	WCHAR m_pwstrThread[DOWNPROP_MAX_THREAD_DIGITALS];
	wstring m_wstrSaveFolder;

	CTreeViewCtrl m_treeFileList;
	
};


class CDownExportLinkPage : public CPropertyPageImpl<CDownExportLinkPage>,
					        public CWinDataExchange<CDownExportLinkPage>
{
public:
	enum { IDD = IDD_EXPROT_LINKS };

	BEGIN_DDX_MAP(CDownExportLinkPage)


    END_DDX_MAP()

	BEGIN_MSG_MAP(CDownExportLinkPage)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)

		COMMAND_ID_HANDLER(IDC_RADIO_SENDFLASHGET, OnRadioExportType);
		COMMAND_ID_HANDLER(IDC_RADIO_SAVEDISK, OnRadioExportType);
		COMMAND_ID_HANDLER(IDC_BUTTON_BROSE_LIST_PATH, OnBrowseListPath)

		CHAIN_MSG_MAP(CPropertyPageImpl<CDownExportLinkPage>)
	END_MSG_MAP()

public:
	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	LRESULT OnRadioExportType(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnBrowseListPath(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

public:
	CDownExportLinkPage();
	~CDownExportLinkPage();

	void RefreshSettings();
	
	//void OnDataExchangeError(UINT nCtrlID, BOOL bSave);
	//void OnDataValidateError(UINT nCtrlID, BOOL bSave, _XData& data);


public:
	CEdit m_edtLinks;
	CButton m_radSendFlash, m_radSaveDisk;
	CButton m_radExpHttp, m_radExpFTP;
	CButton m_radEncGB, m_radEncUTF;
	CEdit m_edtListSavePath;
	CButton m_btnBrowsPath;
	CButton m_chkSaveDefault;

	wstring m_wstrListPath;

	vector<CCURLHelper::PAGE_LINK_ITEM*>* m_pvecLinksData;
};

class CDownPropertyDiag : public CPropertySheetImpl<CDownPropertyDiag>
{
public:
	typedef enum _PAGE_INDEX {
		PAGE_CANCEL = 0,
		PAGE_DOWNPROPERTY,
		PAGE_EXPORTLINK
	} PAGE_INDEX;

public:

	BEGIN_MSG_MAP(CDownPropertyDiag)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)

		CHAIN_MSG_MAP(CPropertySheetImpl<CDownPropertyDiag>)
	END_MSG_MAP()

public:
	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

public:
	CDownPropertyDiag(ATL::_U_STRINGorID pgTitle = (LPCTSTR)NULL, UINT uStartPage = 0, HWND hWndParent = NULL);

	void SetLinksData(vector<CCURLHelper::PAGE_LINK_ITEM*>* pvecLinkData);

public:
	vector<CCURLHelper::PAGE_LINK_ITEM*>* m_pvecLinksData;

	CDownPropertyPage		m_pageDownProp;
	CDownExportLinkPage		m_pageExpLink;

	PAGE_INDEX m_pgCurrent;
};